﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Help;
using Bll;
using System.Data;
using Model;

namespace Myproject.BSM.Ashx.PcChannel
{
    /// <summary>
    /// ArticleDelete 的摘要说明
    /// </summary>
    public class ArticleDelete : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string id = context.Request.QueryString["id"];
            DataSet DS = new ArticleBLL().GetList(" ParentID=" + id);
            if (DS.Tables[0].Rows.Count > 0)
            {
                context.Response.Write(ReturnJsonHelp.OutHandlerJson(false, "删除频道失败,有子频道！"));
            }
            else
            {
                DataSet DS2 = new NewsBLL().GetList(" WebChannelId =" + id);
                if (DS2.Tables[0].Rows.Count > 0)
                {
                    context.Response.Write(ReturnJsonHelp.OutHandlerJson(false, "删除频道失败,频道下有图文信息！"));
                }
                else
                {

                    ArticleModel model2 = new ArticleBLL().GetModel(int.Parse(id));
                    ArticleModel model3 = new ArticleBLL().GetModel(model2.ParentId);//父频道

                    if (new ArticleBLL().Delete(int.Parse(id)))
                    {

                        //修改父频道结束标示[删除子频道，判断父频道是否还有子频道]

                        DataSet DS3 = new ArticleBLL().GetList(" ParentID=" + model3.WebArticleId);
                        if (DS3.Tables[0].Rows.Count == 0)
                        {
                            model3.IsEnd = true;
                            new ArticleBLL().Update(model3);
                        }

                        context.Response.Write(ReturnJsonHelp.OutHandlerJson(true, "删除频道成功！"));
                    }
                    else
                    {
                        context.Response.Write(ReturnJsonHelp.OutHandlerJson(false, "删除频道失败！"));
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}